package jee.boot.module.mail.config;

import jee.boot.module.mail.dao.SysMailDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
 * 初始化邮件环境
 */
@Component
public class InitMail implements ApplicationRunner {
    /**
     * 日志对象
     */
    protected Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private SysMailDao mailDao;
    @Value("${spring.schema}")
    private String schema;

    static {
        // 解决邮件附件名字过长导致的文件名变更为.bin的问题
        System.setProperty("mail.mime.splitlongparameters", "false");
    }
    /**
     * 初始化邮件存储的表结构
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
        {
            logger.info("邮件服务初始化");
            int exist = mailDao.existTable(schema);
            if (exist > 0) {
                return;
            }else{
                logger.info("初始建表:"+schema+".st_mail");
                mailDao.createTable();
            }
        }
    }
}
